package com.slmn.visitor.gui;

import java.util.ArrayList;
import java.util.List;

import javafx.scene.layout.Pane;

public abstract class Composite extends Component {
	// Maybe need ObservableList
	protected final List<Component> children = new ArrayList<>();

	public final boolean addAll(Component... components) {
		boolean addAll = true;
		for (Component c : components) {
			if (isValid(c)) {
				this.add(c);
			} else {
				logger.error("Can't add component {} to this {}.", c, this);
			}
			c.setParent(this);
		}
		return addAll;
	}

	public final boolean removeAll(Component... components) {
		boolean removeAll = true;
		for (Component c : components) {
			if (isValid(c)) {
				this.children.add(c);
				((Pane) this.getNode()).getChildren().remove(c.getNode());
			} else {
				logger.error("Can't remove component {} from this {}.", c, this);
			}
			c.setParent(this);
		}
		return removeAll;
	}

	protected abstract boolean isValid(Component c);
	
	protected abstract boolean add(Component c);
	
	protected abstract boolean remove(Component c);

	public final int getIndexOf(Component c){
		return children.indexOf(c);
	}

}

